<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>用户管理系统</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/bootstrap/5.3.0/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .container { margin-top: 20px; }
        .form-group { margin-bottom: 15px; }
        .table { margin-top: 20px; }
    </style>
</head>
<body>
    <div class="container">
        <h2 class="mb-4">用户管理系统</h2>
        
        <!-- 用户表单 -->
        <div class="card mb-4">
            <div class="card-header">
                <ul class="nav nav-tabs card-header-tabs">
                    <li class="nav-item">
                        <a class="nav-link active" data-bs-toggle="tab" href="#register">注册</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" data-bs-toggle="tab" href="#login">登录</a>
                    </li>
                </ul>
            </div>
            <div class="card-body">
                <div class="tab-content">
                    <!-- 注册表单 -->
                    <div class="tab-pane fade show active" id="register">
                        <form id="registerForm">
                            <div class="form-group">
                                <label>用户名：</label>
                                <input type="text" class="form-control" name="username" required>
                            </div>
                            <div class="form-group">
                                <label>密码：</label>
                                <input type="password" class="form-control" name="password" required>
                            </div>
                            <div class="form-group">
                                <label>姓名：</label>
                                <input type="text" class="form-control" name="name" required>
                            </div>
                            <div class="form-group">
                                <label>年龄：</label>
                                <input type="number" class="form-control" name="age" required>
                            </div>
                            <button type="submit" class="btn btn-primary">注册</button>
                        </form>
                    </div>
                    
                    <!-- 登录表单 -->
                    <div class="tab-pane fade" id="login">
                        <form id="loginForm">
                            <div class="form-group">
                                <label>用户名：</label>
                                <input type="text" class="form-control" name="username" required>
                            </div>
                            <div class="form-group">
                                <label>密码：</label>
                                <input type="password" class="form-control" name="password" required>
                            </div>
                            <button type="submit" class="btn btn-primary">登录</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>

        <!-- 用户列表 -->
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h5 class="mb-0">用户列表</h5>
                <button class="btn btn-primary" id="refreshButton">刷新列表</button>
            </div>
            <div class="card-body">
                <table class="table">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>用户名</th>
                            <th>姓名</th>
                            <th>年龄</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody id="userList"></tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- 编辑用户模态框 -->
    <div class="modal fade" id="editModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">编辑用户</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <form id="editForm">
                        <input type="hidden" name="username">
                        <div class="form-group">
                            <label>姓名：</label>
                            <input type="text" class="form-control" name="name" required>
                        </div>
                        <div class="form-group">
                            <label>年龄：</label>
                            <input type="number" class="form-control" name="age" required>
                        </div>
                        <div class="form-group">
                            <label>新密码：</label>
                            <input type="password" class="form-control" name="password">
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" onclick="updateUser()">保存</button>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.bootcdn.net/ajax/libs/bootstrap/5.3.0/js/bootstrap.bundle.min.js"></script>
    <script>
        // 添加基础URL配置
        const BASE_URL = 'http://localhost:8080';
        
        // 在DOM加载完成后执行
        document.addEventListener('DOMContentLoaded', function() {
            // 绑定刷新按钮事件
            document.getElementById('refreshButton').addEventListener('click', refreshUserList);
            
            // 页面加载时获取用户列表
            refreshUserList();
        });

        // 注册表单提交
        document.getElementById('registerForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            const formData = new FormData(e.target);
            const user = Object.fromEntries(formData.entries());
            
            try {
                const response = await fetch(`${BASE_URL}/api/user/register`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(user)
                });
                const result = await response.json();
                alert(result.message);
                if (result.code === 200) {
                    refreshUserList();
                }
            } catch (error) {
                alert('注册失败：' + error);
            }
        });

        // 登录表单提交
        document.getElementById('loginForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            const formData = new FormData(e.target);
            const user = Object.fromEntries(formData.entries());
            
            try {
                const response = await fetch(`${BASE_URL}/api/user/login`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(user)
                });
                const result = await response.json();
                alert(result.message);
            } catch (error) {
                alert('登录失败：' + error);
            }
        });

        // 刷新用户列表
        async function refreshUserList() {
            try {
                const response = await fetch(`${BASE_URL}/api/user/all`);
                if (!response.ok) {
                    throw new Error(`HTTP error! status: ${response.status}`);
                }
                const wrapper = await response.json();
                const tbody = document.getElementById('userList');
                tbody.innerHTML = '';
                
                if (wrapper.code === 200 && Array.isArray(wrapper.result)) {
                    wrapper.result.forEach(user => {
                        const tr = document.createElement('tr');
                        tr.innerHTML = `
                            <td>${user.id}</td>
                            <td>${user.username}</td>
                            <td>${user.name}</td>
                            <td>${user.age}</td>
                            <td>
                                <button class="btn btn-sm btn-primary" onclick="editUser('${user.username}', '${user.name}', ${user.age})">编辑</button>
                                <button class="btn btn-sm btn-danger" onclick="deleteUser('${user.username}')">删除</button>
                            </td>
                        `;
                        tbody.appendChild(tr);
                    });
                } else {
                    console.error('返回的数据格式错误:', wrapper);
                    alert('获取用户列表失败：' + wrapper.message);
                }
            } catch (error) {
                console.error('获取用户列表失败:', error);
                alert('获取用户列表失败：' + error.message);
            }
        }

        // 删除用户
        async function deleteUser(username) {
            if (!confirm('确定要删除该用户吗？')) return;
            
            try {
                const response = await fetch(`${BASE_URL}/api/user/deleteUser`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(username)
                });
                const result = await response.json();
                alert(result.message);
                if (result.code === 200) {
                    refreshUserList();
                }
            } catch (error) {
                alert('删除失败：' + error);
            }
        }

        // 编辑用户
        let editModal;
        function editUser(username, name, age) {
            if (!editModal) {
                editModal = new bootstrap.Modal(document.getElementById('editModal'));
            }
            const form = document.getElementById('editForm');
            form.username.value = username;
            form.name.value = name;
            form.age.value = age;
            form.password.value = '';
            editModal.show();
        }

        // 更新用户
        async function updateUser() {
            const form = document.getElementById('editForm');
            const formData = new FormData(form);
            const user = Object.fromEntries(formData.entries());
            
            try {
                const response = await fetch(`${BASE_URL}/api/user/updateUser`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(user)
                });
                const result = await response.json();
                alert(result.message);
                if (result.code === 200) {
                    editModal.hide();
                    refreshUserList();
                }
            } catch (error) {
                alert('更新失败：' + error);
            }
        }
    </script>
</body>
</html> 